const Router = require('koa-router')
const dev = process.env.NODE_ENV !== 'production'
const LRUCache = require('lru-cache')

const ssrCache = new LRUCache({
  max: 100,
  // maxAge: 1000 * 60 * 60, // 1hour
})
function myRouter(app) {
  const handler = app.getRequestHandler();
  
  function getCacheKey(ctx) { return ctx.url }
  async function renderAndCache(ctx, pagePath, noCache = 'noCache' , queryParams = null) {
    
    queryParams = ctx.query;
    console.log(queryParams)
    if (dev ) ssrCache.clear()
    if (noCache === 'noCache') {
      //  handler(ctx.req, ctx.res, {
      //   pathname:'/a',
      //   query: {id}
      // })
      // console.log(app);
      console.log(Object.assign(queryParams, ctx.params))
      console.info(`no cache:  is page ${pagePath}, is url ${ctx.url}`)
      await handler(ctx.req, ctx.res, {
          pathname: pagePath,
          query: Object.assign(queryParams, ctx.params)
      })
      // await app.render(ctx.req, ctx.res, pagePath, Object.assign(queryParams, ctx.params))
      //   .then((html) => {
      //     // Let's cache this page
      //     console.info(`no cache:  is page ${pagePath}, is url ${ctx.url}`)
      //     console.log(html);
      //     ctx.body = html
      //   })
      //   .catch((err) => {
      //     console.info('ERRR', err)
      //     return app.renderError(err, ctx.req, ctx.res, pagePath, queryParams)
      //   })
        // await app.renderToHTML(ctx.req, ctx.res, pagePath, Object.assign(queryParams, ctx.params))
        // .then((html) => {
        //   // Let's cache this page
        //   console.info(`no cache:  is page ${pagePath}, is url ${ctx.url}`)
        //   console.log(html);
        //   ctx.body = html
        // })
        // .catch((err) => {
        //   console.info('ERRR', err)
        //   return app.renderError(err, ctx.req, ctx.res, pagePath, queryParams)
        // })
      ctx.respond = false
        
    } else {
      const key = getCacheKey(ctx.req)
      // If we have a page in the cache, let's serve it
      if (ssrCache.has(key)) {
        console.info(`CACHE HIT: ${key}`)
        ctx.body = ssrCache.get(key)
        return Promise.resolve()
      }
    
      // If not let's render the page into HTML
      await app.render(ctx.req, ctx.res, pagePath, queryParams)
        .then((html) => {
          // Let's cache this page
          console.info(`CACHE MISS: ${key}`)
          ssrCache.set(key, html)
          ctx.body = html
        })
        .catch((err) => {
          console.info('ERRR', err)
          return app.renderError(err, ctx.req, ctx.res, pagePath, queryParams)
        })
      ctx.respond = false;
    }
  }
  const router = new Router();
  /**
   * 不可以跟 proxyTable 代理重名
   */
  router.get('/', async (ctx) => await renderAndCache(ctx, '/index'))
  router.get('/index', async (ctx) => await renderAndCache(ctx, '/index'))
  router.get('/index/:id', async (ctx) => await renderAndCache(ctx, '/index'))
  router.get('/test2', async (ctx) => await renderAndCache(ctx, '/test1'))
  router.get('/about', async (ctx) => await renderAndCache(ctx, '/about'))
  // router.get('/guanzhu', async (ctx) => await renderAndCache(ctx, '/guanzhu'))
  // router.get('/hotelrankings/:cityId/:type/:divide', async (ctx) => await renderAndCache(ctx, '/hotelrankings'))
  // router.get('/hotelrankings', async (ctx) => await renderAndCache(ctx, '/hotelrankings'))
  // // pc 触屏互跳
  // router.get('/hotel_rankings/:page/:showCount/:year/:month/:cityId/:type/:divide', async (ctx) => await renderAndCache(ctx, '/hotelrankings'))
  // // 文旅
  // router.get('/brandrankings/1105/359', async (ctx) => await renderAndCache(ctx, '/tourismlist'))
  // router.get('/brandrankings/:indexType/:typeId', async (ctx) => await renderAndCache(ctx, '/brandrankings'))
  // router.get('/brandrankings/:indexType/:typeId/:scenicId', async (ctx) => await renderAndCache(ctx, '/brandrankings'))
  // router.get('/hotel_rankings', async (ctx) => await renderAndCache(ctx, '/hotelrankings'))
  // // pc 触屏互跳
  // router.get('/brand_rankings', async (ctx) => await renderAndCache(ctx, '/brandrankings'))
  // router.get('/brand_rankings/:page/:showCount/:year/:month/:typeId/:indexType', async (ctx) => await renderAndCache(ctx, '/brandrankings'))
  // router.get('/brand_rankings/:page/:showCount/:year/:month/:typeId/:indexType/:scenicId', async (ctx) => await renderAndCache(ctx, '/brandrankings'))
  
  // router.get('/brandpage/1105/359/:pagesNum', async (ctx) => await renderAndCache(ctx, '/tourismbrandpage'))
  // router.get('/brandpage/:indexType/:typeId/:pagesNum', async (ctx) => await renderAndCache(ctx, '/brandpage'))
  // router.get('/brandpage/:indexType/:typeId/:scenicId/:pagesNum', async (ctx) => await renderAndCache(ctx, '/brandpage'))
  // router.get('/brandpage', async (ctx) => await renderAndCache(ctx, '/brandpage'))
  // router.get('/hotelPage/:cityId/:type/:divide/:pagesNum', async (ctx) => await renderAndCache(ctx, '/hotelPage'))
  // router.get('/pk', async (ctx) => await renderAndCache(ctx, '/pk'))
  // router.get('/tourismpk', async (ctx) => await renderAndCache(ctx, '/tourismpk'))
  // router.get('/brandindex/:id', async (ctx) => await renderAndCache(ctx, '/brandindex'))
  // router.get('/hotelindex/:id', async (ctx) => await renderAndCache(ctx, '/hotelindex'))
  // router.get('/brandcomment/:id', async (ctx) => await renderAndCache(ctx, '/brandcomment'))
  // router.get('/mediabrandcomment/:brandId/:pagesNum', async (ctx) => await renderAndCache(ctx, '/mediabrandcomment'))
  // router.get('/brandcomment/:id/:pagesNum', async (ctx) => await renderAndCache(ctx, '/brandcomment'))
  // router.get('/hotelcomment/:id', async (ctx) => await renderAndCache(ctx, '/hotelcomment'))
  // router.get('/hotelcomment/:id/:pagesNum', async (ctx) => await renderAndCache(ctx, '/hotelcomment'))
  // router.get('/shareindexmci', async (ctx) => await renderAndCache(ctx, '/share/shareindexmci'))
  // router.get('/sharemciranking', async (ctx) => await renderAndCache(ctx, '/share/sharemciranking'))
  // router.get('/shareindexmbi', async (ctx) => await renderAndCache(ctx, '/share/shareindexmbi'))
  // router.get('/sharembiranking', async (ctx) => await renderAndCache(ctx, '/share/sharembiranking'))
  // router.get('/mciMoreRankings/:id', async (ctx) => await renderAndCache(ctx, '/mciMoreRankings'))
  // router.get('/mciMoreRankings', async (ctx) => await renderAndCache(ctx, '/mciMoreRankings'))
  // router.get('/mciCustomDetail/:id', async (ctx) => await renderAndCache(ctx, '/mciCustomDetail'))
  // router.get('/shareMciCustomDetail/:id', async (ctx) => await renderAndCache(ctx, '/share/shareMciCustomDetail'))

  // // mvi 酒店 景区 排名
  // router.get('/mvi_rankings', async (ctx) => await renderAndCache(ctx, '/mvi/wineshopMviRankings'))
  // router.get('/mvi_rankings/1107/:page/:showCount/:year/:month', async (ctx) => await renderAndCache(ctx, '/mvi/wineshopMviRankings'))
  // router.get('/mvi_rankings/1108', async (ctx) => await renderAndCache(ctx, '/mvi/scenicSpotMviRankings'))
  // router.get('/mvi_rankings/1108/:page/:showCount/:year/:month', async (ctx) => await renderAndCache(ctx, '/mvi/scenicSpotMviRankings'))
  // router.get('/mviBrandIndexDetail/:id/1107', async (ctx) => await renderAndCache(ctx, '/mvi/wineshopIndex'))
  // router.get('/mviBrandIndexDetail/:id/1108', async (ctx) => await renderAndCache(ctx, '/mvi/scenicSpotIndex'))
  // router.get('/mviBill/:id/1107', async (ctx) => await renderAndCache(ctx, '/share/mviBill'))
  // router.get('/mviBill/:id/1108', async (ctx) => await renderAndCache(ctx, '/share/mviBill'))
  // // 文旅
  // router.get('/getTourismMbiIndex/:id', async (ctx) => await renderAndCache(ctx, '/tourismRankDate'))
  // router.get('/mediaTourismComment/:brandId/:pagesNum', async (ctx) => await renderAndCache(ctx, '/mediaTourismComment'))
  return router
}




module.exports = myRouter;